﻿#region

using System;
using System.IO;
using System.Net.Mail;
using System.Threading.Tasks;
using System.Web;
using System.Web.Routing;
using YuYuKiCms.Framework.Interface;

#endregion

namespace YuYuKiCms.Framework.Core
{
    public class Util : IUtil
    {
        #region Property

        public string AppFolder { get; set; }
        public string ThemeFolder { get; set; }
        public string ThemeViewFolder { get; set; }

        #endregion

        #region Method

        public string GetAppPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        public void SetCookie(HttpResponseBase response, string lg)
        {
            if (response != null)
            {
                var cookie = new HttpCookie("lg")
                {
                    Value = lg,
                    Expires = DateTime.Now.AddYears(3),
                    HttpOnly = true
                };
                response.Cookies.Add(cookie);
            }
        }

        public void SendMail(string email, string from, string subject, string body)
        {
            Task.Run(() =>
            {
                var client = new SmtpClient();
                var mail = new MailMessage
                {
                    From = new MailAddress(from),
                    Subject = subject,
                    Body = body,
                    IsBodyHtml = true
                };

                mail.To.Add(email);

                client.Send(mail);
            });
        }

        public string GetThemeFolder()
        {
            return ThemeFolder ??
                   (ThemeFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Themes"));
        }

        public string GetRouteData(string key)
        {
            var httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
            var routeData = RouteTable.Routes.GetRouteData(httpContextWrapper);
            if (null == routeData || !routeData.Values.ContainsKey(key))
                return null;

            return routeData.Values[key].ToString();
        }

        #endregion
    }
}